package helixoide.users;

import helixoide.hxLog;
import helixoide.irc.serverType.unreal32;
import java.util.ArrayList;
import java.util.ListIterator;
//used to manage connections to users
//that means this class will have to be used by the sl component and the irc component
//REMEMBER TO SWITCH THIS TO ARRAYLIST TONIGHT!!!!!
//that is done: rdelinger Fri 9:52 jan 15,2009
public class UserManager 
{
	private ArrayList<User> userList = new ArrayList<User>();
	private unreal32 protocol;
	public UserManager(unreal32 proto)
	{
		protocol = proto;
	}
	public synchronized boolean newUser(String name, String hostname, String userName, String realName, String uuid, String channel,boolean object, String dUUID)
	{
		User theUser = this.findUser(name);
		boolean success = false;
		if(theUser==null)
		{
			//no nick found make one
			this.addUser(userName, name, hostname, uuid, channel, realName, protocol,object,dUUID);
			success = true;
		}
		return success;
	}
	public synchronized User[] getUsers()
	{
		User[] users = new User[this.userList.size()];
		this.userList.toArray(users);
		return users;
	}
	public synchronized String[] getNicks()
	{
		User[] temp = this.getUsers();
		ArrayList<String> nicks = new ArrayList<String>();
		for(int i = 0;i<temp.length;i++)
		{
			nicks.add(temp[i].getNick());
		}
		String[] theNicks = new String[nicks.size()];
		nicks.toArray(theNicks);
		return theNicks;
		
	}
	public synchronized User findUser(String nick)
	{
		User[] listOfUsers = this.getUsers();
		User toReturn = null;
		for(int i = 0;i<listOfUsers.length;i++)
		{
			
			if(listOfUsers[i].getNick().equals(nick))
			{
				toReturn = listOfUsers[i];
				break;
			}
			
		}
		return toReturn;
	}
	private User addUser(String userName, String nickName, String hostName, String uuid, String Channel, String realName, unreal32 proto, boolean object, String dUUID)
	{
		//return a the newly created user, or if the user already exists, return that user
		User[] currentUsers = this.getUsers();
		boolean found = false;
		User theUser = null;
		for(int i = 0;i<currentUsers.length;i++)
		{
			if(currentUsers[i].getRealName().equals(realName))//check the uuid in the realname
			{
				found = true;
				theUser = currentUsers[i];
				break;
			}
		}
		if(found==false)
		{
			theUser = new User(userName,nickName,hostName,uuid,Channel,realName,proto,object,dUUID);//create the user
			userList.add(theUser);// add him to the list
		}
		return theUser;
	}
	public void removeUser(String user, String uuid, String channel, String reason)
	{
		if(reason == null)
		{
			reason = "Left Chat Range";
		}
	    User toRemove = this.findUser(user);
	    if(toRemove!=null)
		{
			//this is short sited we need to make it dynamic so there is a list of UUIDs
			if(toRemove.getDeviceUUID()==uuid)//check to see if we have permission to remove this user
			{
				if(toRemove.numberOfChannels()<=1) 
				{
					toRemove.quit(reason);
					this.userList.remove(toRemove);
					hxLog.write("UserManager", "Error: Device " +uuid+ " made User " +toRemove.getNick()+ " QUIT since the user was in " +toRemove.numberOfChannels() +" Channels\n");
					
				}
				else if(toRemove.numberOfChannels()>1)
				{
					toRemove.part(channel, reason);
					hxLog.write("UserManager", "Error: Device " +uuid+ " made User " +toRemove.getNick()+ " PART since the user was in " +toRemove.numberOfChannels() +" Channels\n");
				}
			}
			else
			{
				hxLog.write("UserManager", "Error: Device "+uuid+" tried to illegally part/quit user " +toRemove.getNick() +"\n");
			}
		}
		else
		{
			hxLog.write("UserManager", "Error: The user we want to remove is NULL\n");
		}
		
	}
	public void removeUser(User theUser)//this is mainly used for objects
	{
		if(theUser!=null)
		{
			theUser.quit("Left Chat Range");
			this.userList.remove(theUser);
		}
	}
}